package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;

import com.metagurukul.informer.service.RegisterUserService;
import com.metagurukul.informer.utility.ExceptionProperties;
import com.metagurukul.informer.utility.SendMail;
import com.metagurukul.informer.utility.Validator;

/**
 * Servlet implementation class RegisterUser
 */
public class RegisterUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

/**
 * this function register user if its not already a member
 * send password message to user account for further logged in process
 * 
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	protected void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Validator validator = new Validator();
		Map<String, String> exceptionmap = new HashMap<String, String>();
		String emailsignup = request.getParameter("emailsignup");
		String usernamesignup = request.getParameter("usernamesignup");
		
		if (emailsignup == null) {
			exceptionmap.put("emailsignup", ExceptionProperties._101);
		} else {
			if (emailsignup == "") {
				exceptionmap.put("emailsignup", ExceptionProperties._106);
			} else {
				if (!validator.emailValidate(emailsignup)) {
					exceptionmap.put("emailsignup", ExceptionProperties._102);
				}
			}
		}
		if (usernamesignup == null) {
			exceptionmap.put("usernamesignup", ExceptionProperties._101);
		} else {
			if (usernamesignup == "") {
				exceptionmap.put("usernamesignup", ExceptionProperties._106);
			} else {
				if (!validator.nameValidator(usernamesignup)) {
					exceptionmap.put("usernamesignup", ExceptionProperties._103);
				}
			}
		}
		
		//if there is no validation failure
		if (exceptionmap.size() == 0) {
			emailsignup = emailsignup.trim();
			RegisterUserService registeruserservice = new RegisterUserService(emailsignup, usernamesignup);
			
			try {
				//user is registerd but not logged in even once so not a member than 
				//it delete old entry from temp user and registered as a new user 
				if(registeruserservice.isTempUser()){
					registeruserservice.deleteTempUser();
					String password=registeruserservice.doRegisterUser();
					if(password==null){
						response.sendRedirect("Error.jsp");
					}else{
						
						SendMail sendmail = new SendMail(); 
						sendmail.sendMail(emailsignup, "Registration",
								  "Welcome  " + usernamesignup +
								  " you are registered successfully for our website metainformer.com. your password id : "+password);
						
						/*create session for AfterRegistration.jsp  page to 
						*check that if the request is coming from RegisterUser page*/
						
						request.setAttribute("useremail", emailsignup);
						HttpSession session=request.getSession();
						session.setAttribute("registered", "ok");
						response.sendRedirect("AfterRegistration.jsp");
					}
					//if user has not registered yet
				}else if(! registeruserservice.isUserExist()){
					
					String password=registeruserservice.doRegisterUser();
					if(password==null){
						response.sendRedirect("Error.jsp");
					}else{
					
						SendMail sendmail = new SendMail(); 
						sendmail.sendMail(emailsignup, "Registration",
								  "Welcome  " + usernamesignup +
								  " you are registered successfully for our website metainformer.com. your password id : "+password);
						
						/*create session for AfterRegistration.jsp  page to check that if the reqest is coming 
						 * from RegisterUser page*/
						request.setAttribute("useremail", emailsignup);
						HttpSession session=request.getSession();
						session.setAttribute("registered", "ok");
						response.sendRedirect("AfterRegistration.jsp");
					}
					
				}else{
					exceptionmap.put("emailsignup",ExceptionProperties._107 );
					request.setAttribute("exceptionmap", exceptionmap);
					request.getRequestDispatcher("Register.jsp").forward(request, response);
				}
				
			} catch (Exception e) {
				response.sendRedirect("Error.jsp");
			}
			
		}else{
			
			request.setAttribute("exceptionmap", exceptionmap);
			request.getRequestDispatcher("Register.jsp").forward(request, response); 
		}
	

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

}
